package com.yitiao.data_algorithms.leetcode.editor.cn;

import java.util.Arrays;

class CheckIfNAndItsDoubleExist {
    // 2023-04-03 16:27:41
    public static void main(String[] args) {
        Solution solution = new CheckIfNAndItsDoubleExist().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean checkIfExist(int[] arr) {
            Arrays.sort(arr);
            // 二分查找
            for (int i = 0; i < arr.length; i++) {
                int left = 0;
                int right = arr.length - 1;
                if (arr[i] >= 0) {
                    left = i + 1;//arr大于零l为i+1
                } else {
                     right= i - 1;
                }//arr小于零r为i-1
                while (left <= right) {
                    int mid = left + (right - left) / 2;
                    if (arr[mid] == arr[i] * 2) {
                        return true;
                    } else if (arr[mid] < arr[i] * 2) {
                        left = mid + 1;
                    } else {
                        right = mid - 1;
                    }
                }
            }
            return false;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}